package com.pshcp.api;

import com.external.hospital.im.Hospital2ImService;
import com.external.hospital.im.domain.ConnectParamsBO;
import com.external.hospital.im.domain.ConnectParamsDTO;
import com.external.hospital.im.domain.FinishConsultSessionDTO;
import com.external.hospital.im.domain.InitConsultSessionBO;
import com.external.hospital.im.domain.InitConsultSessionDTO;
import com.external.hospital.im.domain.SendMessageBO;
import com.external.hospital.im.domain.SendMessageDTO;
import com.external.hospital.im.domain.SendMessageListBO;
import com.external.hospital.im.domain.SendMessageListDTO;
import com.framework.base.PageList;
import com.framework.base.ResultBuilder;
import com.framework.base.ResultDTO;
import com.framework.exception.BizException;
import com.framework.logaop.Log;
import com.framework.logaop.LogSourceEnum;
import com.framework.logaop.LogTypeEnum;
import com.framework.thread.ReqComTool;
import com.framework.tool.CommonTool;
import com.pshcp.ao.im.ConnectParamsAO;
import com.pshcp.ao.im.FinishConsultSessionAO;
import com.pshcp.ao.im.InitConsultSessionAO;
import com.pshcp.ao.im.SendMessageAO;
import com.pshcp.ao.im.SendMessagePageAO;
import com.pshcp.domain.model.OnlineSession;
import com.pshcp.service.OnlineSessionService;
import com.pshcp.tool.ImTool;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

/**
 * @Description: TODO
 * @Author: xiaoche
 * @Date: 2022/12/10 14:46
 */
@Api(tags = {"通用接口-IM"})
@Slf4j
@RestController
@RequestMapping("/api/im")
public class ImAPI {

    @Autowired
    private Hospital2ImService imService;
    @Autowired
    private OnlineSessionService onlineSessionService;

    @ApiOperation("获取IM连接配置")
    @Log(source = LogSourceEnum.APP, type = LogTypeEnum.SELECT, desc = "获取IM连接配置")
    @PostMapping("getConnectParams")
    public ResultDTO<ConnectParamsBO> getConnectParams(@RequestBody ConnectParamsAO connectParamsAO) throws Exception {
        CommonTool.checkObj(connectParamsAO.getPatientOnlineOrderId(), "90000006");
        OnlineSession onlineSession = onlineSessionService.getOnlineSessionByPatientOnlineOrderId(connectParamsAO.getPatientOnlineOrderId());
        CommonTool.checkObj(onlineSession, "90000007");
        ConnectParamsDTO connectParamsDTO = ImTool.buildConnectParamsDTO(onlineSession);
        ConnectParamsBO connectParamsBO = imService.connectParams(connectParamsDTO);
        return ResultBuilder.success(connectParamsBO);
    }


    @ApiOperation("初始化咨询会话")
    @Log(source = LogSourceEnum.APP, type = LogTypeEnum.UPDATE, desc = "初始化咨询会话")
    @PostMapping("initConsultSession")
    public ResultDTO<String> initConsultSession(@RequestBody InitConsultSessionAO initConsultSessionAO) throws BizException {
        CommonTool.checkObj(initConsultSessionAO.getPatientOnlineOrderId(), "90000006");
        OnlineSession onlineSession = onlineSessionService.getOnlineSessionByPatientOnlineOrderId(initConsultSessionAO.getPatientOnlineOrderId());
        CommonTool.checkObj(onlineSession, "90000007");
        if (!StringUtils.isBlank(onlineSession.getSessionId())) {
            return ResultBuilder.success(onlineSession.getSessionId());
        }
        InitConsultSessionDTO initConsultSessionDTO = ImTool.buildInitConsultSession(onlineSession);
        InitConsultSessionBO initConsultSessionBO = imService.initConsultSession(initConsultSessionDTO);
        OnlineSession updateOnlineSession = ImTool.buildInit2UpdateOnlineSession(onlineSession, initConsultSessionBO);
        onlineSessionService.update(updateOnlineSession);
        return ResultBuilder.success(updateOnlineSession.getSessionId());
    }

    @ApiOperation("结束咨询会话")
    @Log(source = LogSourceEnum.APP, type = LogTypeEnum.UPDATE, desc = "结束咨询会话")
    @PostMapping("finishConsultSession")
    public ResultDTO<String> finishConsultSession(@RequestBody FinishConsultSessionAO finishConsultSessionAO) throws BizException {
        if (ReqComTool.getUserType() != 0) {
            return ResultBuilder.failure("90000014");
        }
        CommonTool.checkObj(finishConsultSessionAO.getPatientOnlineOrderId(), "90000006");
        OnlineSession onlineSession = onlineSessionService.getOnlineSessionByPatientOnlineOrderId(finishConsultSessionAO.getPatientOnlineOrderId());
        CommonTool.checkObj(onlineSession, "90000007");
        if (onlineSession.getSessionStatus() >= 2) {
            return ResultBuilder.failure("90000011");
        }
        FinishConsultSessionDTO finishConsultSessionDTO = ImTool.buildFinishConsultSession(onlineSession);
        imService.finishConsultSession(finishConsultSessionDTO);
        OnlineSession updateOnlineSession = ImTool.buildFinish2UpdateOnlineSession(onlineSession);
        onlineSessionService.update(updateOnlineSession);
        return ResultBuilder.success(null);
    }

    @ApiOperation("发送IM消息")
    @Log(source = LogSourceEnum.APP, type = LogTypeEnum.UPDATE, desc = "发送IM消息")
    @PostMapping("sendMessage")
    public ResultDTO<SendMessageBO> sendMessage(SendMessageAO sendMessageAO, @ApiParam(value = "附件") @RequestPart("file") MultipartFile file) throws BizException {
        ImTool.checkSendMessage(sendMessageAO, file);
        OnlineSession onlineSession = onlineSessionService.getOnlineSessionBySessionId(sendMessageAO.getSessionId());
        CommonTool.checkObj(onlineSession, "90000007");
        SendMessageDTO sendMessageDTO = ImTool.buildSendMessageDTO(onlineSession, sendMessageAO, file);
        SendMessageBO sendMessageBO = imService.sendMessage(sendMessageDTO);
        OnlineSession updateOnlineSession = ImTool.buildSendMessage2UpdateOnlineSession(onlineSession, sendMessageBO);
        onlineSessionService.update(updateOnlineSession);
        return ResultBuilder.success(sendMessageBO);
    }

    @ApiOperation("获取IM消息(分页)")
    @Log(source = LogSourceEnum.APP, type = LogTypeEnum.SELECT, desc = "获取IM消息(分页)")
    @PostMapping("getSendMessagePage")
    public ResultDTO<PageList<SendMessageListBO>> getSendMessagePage(@RequestBody SendMessagePageAO sendMessagePageAO) throws BizException {
        ImTool.checkGetSendMessagePage(sendMessagePageAO);
        OnlineSession onlineSession = onlineSessionService.getOnlineSessionBySessionId(sendMessagePageAO.getSessionId());
        CommonTool.checkObj(onlineSession, "90000007");
        SendMessageListDTO sendMessageListDTO = ImTool.buildGetSendMessagePage(sendMessagePageAO, onlineSession);
        PageList<SendMessageListBO> pageList = imService.getSendMessageList(sendMessageListDTO);
        return ResultBuilder.success(pageList);
    }
}
